import re
import time

import requests


class SpiderImg:

    def __init__(self):
        self.url='https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=&st=-1&fm=result&fr=&sf=1&fmq=1735647959462_R&pv=&ic=0&nc=1&z=&hd=&latest=&copyright=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&dyTabStr=&ie=utf-8&sid=&word=%E8%80%81%E8%99%8E'
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
        }


    def save_img(self,url_img,filename):
        response = requests.get(url=url_img, headers=self.headers)
        content = response.content
        with open(filename, 'wb') as f:
            f.write(content)



    def get_urls(self):
        response = requests.get(url=self.url, headers=self.headers)
        content = response.content.decode("utf-8")
        urls = re.findall('"hoverURL": "(.*?)"', content, re.DOTALL)
        print(urls)
        num = 1
        for url in urls:

            filename = "./img//tiger{}.jpg".format(num)
            print(filename)
            self.save_img(url,filename)
            num = num + 1



if __name__ == '__main__':
    obj = SpiderImg()
    obj.get_urls();
